package com.example.g5project.customer.controller;

import com.example.g5project.chat.model.enumeration.IdentityEnum;
import com.example.g5project.chat.util.IMUserIdUtils;
import com.example.g5project.chat.util.JsonUtils;
import com.example.g5project.chat.util.TencentImUtils;
import com.example.g5project.customer.model.dto.CustomerDTO;
import com.example.g5project.customer.model.param.CustomerEditParam;
import com.example.g5project.customer.model.param.CustomerLoginParam;
import com.example.g5project.customer.model.param.CustomerRegisterParam;
import com.example.g5project.customer.model.result.Result;
import com.example.g5project.customer.service.CustomerService;
import com.example.g5project.others.ResponseValue;
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/counseling/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private TencentImUtils tencentImUtils;
    @Autowired
    private IMUserIdUtils imUserIdUtils;

    @PostMapping("/register")
    public ResponseValue<Integer> register(@RequestBody CustomerRegisterParam param) {
        Result result = param.check();
        if (!result.isRight()) {
            return ResponseValue.fail("403", result.getMessage());
        }
        Integer id = customerService.register(param);
        if (id == null) {
            return ResponseValue.fail("403", "失败");
        }
        tencentImUtils.accountImport(imUserIdUtils.importUserId(id, IdentityEnum.CUSTOMER));
        return ResponseValue.success(id);
    }

    @GetMapping("/detail")
    public ResponseValue<CustomerDTO> detail(@RequestParam("customerId") Integer customerId) {
        CustomerDTO dto = customerService.detailById(customerId);
        return dto == null ? ResponseValue.fail("404", "不存在") : ResponseValue.success(dto);
    }

    @PostMapping("/detail")
    public ResponseValue<CustomerDTO> detailV2(@RequestBody String param) {
        int customerId;
        Optional<Map<Object, Object>> optional = JsonUtils.decodeJsonToMap(param);
        try {
            Map<Object, Object> map = optional.get();
            customerId = (Integer) map.get("customerId");
        } catch (Exception e) {
            log.error("参数解析错误");
            e.printStackTrace();
            return ResponseValue.fail("403", "参数解析错误");
        }
        CustomerDTO dto = customerService.detailById(customerId);
        return dto == null ? ResponseValue.fail("403", "失败") : ResponseValue.success(dto);
    }

    @PutMapping("/edit")
    public ResponseValue edit(@RequestBody CustomerEditParam param) {
        Result result = param.checkEditParam();
        if (!result.isRight()) {
            return ResponseValue.fail("403", result.getMessage());
        }
        return customerService.edit(param) ? ResponseValue.success(null) : ResponseValue.fail("403", "失败");
    }

    @PostMapping("/login")
    public ResponseValue<Integer> login(@RequestBody CustomerLoginParam param) {
        Result result = param.check();
        if (!result.isRight()) {
            return ResponseValue.fail("403", result.getMessage());
        }
        CustomerDTO dto = customerService.login(param);
        return dto == null ? ResponseValue.fail("404", "失败") : ResponseValue.success(dto.getId());
    }


}
